<?php
namespace addons\encryption\controller;

use app\common\controller\Backend;

class Encryption extends Backend
{
    private static $config = [
        1 => [
            'name' => 'SG11加密',
            'method' => 'Sg11',
            'versionSelection' => '1',
            'introduce' => 'SG11目前支持PHP版本为：最高支持7.4版本&lt;br&gt;&lt;br&gt;&lt;b style=&quot;color:red&quot;&gt; 关于批量加密：&lt;/b&gt;&lt;br&gt;1. 仅支持上传zip后缀的压缩文件，所有目录不能包含中文和其他非法字符。&lt;br&gt;2. 压缩包大小不能超过2M，普通会员一次一个压缩包最多10个文件。&lt;br&gt;3. 一般10秒-60S出结果，等待完整，点击下载，也可在加密记录中下载。&lt;br&gt;SourceGuardian是组件加密，需要安装组件才能运行，不适合虚拟主机上运行。',
            'version' => ['5.4','5.5','5.6','7.0','7.1','7.2','7.3','7.4']
        ],
        2 => [
            'name' => 'SG12加密',
            'method' => 'Sg12',
            'versionSelection' => '1',
            'introduce' => 'SG12目前支持PHP版本为：最高支持8.0版本&lt;br&gt;&lt;br&gt;&lt;b style=&quot;color:red&quot;&gt; 关于批量加密：&lt;/b&gt;&lt;br&gt;1. 仅支持上传zip后缀的压缩文件，所有目录不能包含中文和其他非法字符。&lt;br&gt;2. 压缩包大小不能超过2M，普通会员一次一个压缩包最多10个文件。&lt;br&gt;3. 一般10秒-60S出结果，等待完整，点击下载，也可在加密记录中下载。&lt;br&gt;SourceGuardian是组件加密，需要安装组件才能运行，不适合虚拟主机上运行。&lt;br&gt;Sg13组件
            &lt;br&gt;Windows/Linux/Max OS: &lt;a target=&quot;_blank&quot; href=&quot;https://sf2129876388.lanzouv.com/iTWSG0glnrra&quot;&gt;点击下载&lt;/a&gt;',
            'version' => ['5.4','5.5','5.6','7.0','7.1','7.2','7.3','7.4','8.0']
        ],
        3 => [
            'name' => 'SG13加密',
            'method' => 'Sg13',
            'versionSelection' => '1',
            'introduce' => 'SG13目前支持PHP版本为：最高支持8.1版本&lt;br&gt;&lt;br&gt;&lt;b style=&quot;color:red&quot;&gt; 关于批量加密：&lt;/b&gt;&lt;br&gt;1. 仅支持上传zip后缀的压缩文件，所有目录不能包含中文和其他非法字符。&lt;br&gt;2. 压缩包大小不能超过2M，普通会员一次一个压缩包最多10个文件。&lt;br&gt;3. 一般10秒-60S出结果，等待完整，点击下载，也可在加密记录中下载。&lt;br&gt;SourceGuardian是组件加密，需要安装组件才能运行，不适合虚拟主机上运行。&lt;br&gt;Sg13组件
            &lt;br&gt;Windows/Linux/Max OS: &lt;a target=&quot;_blank&quot; href=&quot;https://sf2129876388.lanzouv.com/iTWSG0glnrra&quot;&gt;点击下载&lt;/a&gt;',
            'version' => ['5.4','5.5','5.6','7.0','7.1','7.2','7.3','7.4','8.0','8.1']
        ]
    ];

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function list(){
        return self::$config;
    }

    public function index()
    {
        if(IS_POST){
            $post = $this->request->post();
            $type = !empty($post['type'])?intval($post['type']):null;
            $version = !empty($post['version'])?$post['version']:null;
            $comment = !empty($post['comment'])?$post['comment']:null;
            $filename = !empty($post['name'])?$post['name']:null;

            if(empty($type)){
                return message('请选择加密类型！', false);
            }

            if(empty($comment)){
                return message('请输入版权信息！', false);
            }

            if(empty($filename)){
                return message('请先上传文件！', false);
            }

            if(!array_key_exists($type, self::$config)){
                return message('不存在此加密类型！', false);
            }

            if(self::$config[$type]['versionSelection'] && empty($version)){
                return message('请选择PHP版本！', false);
            }

            try{
                $zip_name = 'SF_Encryption_'.time().'.zip';
                $filePath = RUNTIME_PATH . DS . 'temp' . DS . 'upload' . DS . $filename;
                $completePath = RUNTIME_PATH . DS . 'temp' . DS . 'upload' . DS . $zip_name;
                $class = '\\addons\\encryption\\library\\' . self::$config[$type]['method'];
                if (!class_exists($class)) {
                    return message('不存在此加密类库！', false);
                }
                $encryptionClass = new $class();
                $result = $encryptionClass->encryption($comment, $version, $filePath, $completePath);
                if($result['code'] == 1){
                    return message('加密成功！', true, $zip_name);
                }else{
                    return message('加密失败，请稍后重试！<br>失败原因：'.$result['info'], false);
                }
            }catch(\Exception $e){
                return message('加密失败！' . $e->getMessage(), false);
            }

        }
    }

    public function download(){
        $name = $this->request->get('name');
        if(empty($name)){
            return $this->render('/public/error', ['msg' => '压缩包名不能为空！']);
        }
        $filePath = RUNTIME_PATH . DS . 'temp' . DS . 'upload' . DS . $name;
        if(!is_file($filePath)){
            return $this->render('/public/error', ['msg' => '不存在此压缩包！']);
        }
        return download($filePath, $name);
    }

    private function get_curl($url, $paras = array()) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if (@$paras['Header']) {
            $Header = $paras['Header'];
        } else {
            $Header[] = "Accept:*/*";
            $Header[] = "Accept-Encoding:gzip,deflate,sdch";
            $Header[] = "Accept-Language:zh-CN,zh;q=0.8";
            $Header[] = "Connection:close";
        }
        curl_setopt($ch, CURLOPT_HTTPHEADER, $Header);
        if (@$paras['ctime']) { // 连接超时
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $paras['ctime']);
        } else {
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        }
        if (@$paras['rtime']) { // 读取超时
            curl_setopt($ch, CURLOPT_TIMEOUT, $paras['rtime']);
        }
        if (@$paras['post']) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $paras['post']);
        }
        if (@$paras['header']) {
            curl_setopt($ch, CURLOPT_HEADER, true);
        }
        if (@$paras['cookie']) {
            curl_setopt($ch, CURLOPT_COOKIE, $paras['cookie']);
        }
        if (@$paras['refer']) {
            if ($paras['refer'] == 1) {
                curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
            } else {
                curl_setopt($ch, CURLOPT_REFERER, $paras['refer']);
            }
        }
        if (@$paras['ua']) {
            curl_setopt($ch, CURLOPT_USERAGENT, $paras['ua']);
        } else {
            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
        }
        if (@$paras['nobody']) {
            curl_setopt($ch, CURLOPT_NOBODY, 1);
        }
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if (@$paras['GetCookie']) {
            curl_setopt($ch, CURLOPT_HEADER, 1);
            $result = curl_exec($ch);
            preg_match_all("/Set-Cookie: (.*?);/m", $result, $matches);
            $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            $header = substr($result, 0, $headerSize); //状态码
            $body = substr($result, $headerSize); //响应内容
            $ret = [
                "Cookie" => $matches, "body" => $body, "code" => $header
            ];
            curl_close($ch);
            return $ret;
        }
        $ret = curl_exec($ch);
        if (@$paras['loadurl']) {
            $Headers = curl_getinfo($ch);
            $ret = $Headers['redirect_url'];
        }
        curl_close($ch);
        return $ret;
    }
}